﻿using System.Data;
using MySql.Data.MySqlClient;

namespace Api.Dal.Tools
{
    //public class SqlHelper
    //{
    //    private static string _conn { get; set; }

    //    static SqlHelper()
    //    {
    //        _conn = "server=.;database=BBS;uid=first;pwd=abcdefg;TrustServerCertificate=true";
    //    }
    //    public static DataTable ExecuteTable(string cmdText, params SqlParameter[] sqlParameters)
    //    {
    //        DataSet ds = new DataSet();
    //        using SqlConnection conn = new SqlConnection(_conn);
    //        conn.Open();
    //        SqlCommand sqlCommand = conn.CreateCommand();
    //        sqlCommand.CommandText = cmdText;
    //        sqlCommand.Parameters.AddRange(sqlParameters);
    //        SqlDataAdapter adapter = new SqlDataAdapter(sqlCommand);
    //        adapter.Fill(ds);
    //        return ds.Tables[0];
    //    }

    //    public static int ExecuteNonQuery(string cmdText, params SqlParameter[] sqlParameters)
    //    {
    //        using SqlConnection conn = new SqlConnection(_conn);
    //        conn.Open();
    //        SqlCommand sqlCommand = conn.CreateCommand();
    //        sqlCommand.CommandText = cmdText;
    //        sqlCommand.Parameters.AddRange(sqlParameters);
    //        return sqlCommand.ExecuteNonQuery();
    //    }
    //}

    public class MySQLHelper
    {
        private static string? _connectionString { get; set; }

        public MySQLHelper()
        {
            _connectionString = "server=localhost;user=root;password=123456;database=bbs";
        }

        

        public static DataTable ExecuteTable(string cmdText, params MySqlParameter[] sqlParameters)
        {
            DataSet ds = new DataSet();
            using MySqlConnection conn = new MySqlConnection(_connectionString); ;
            conn.Open();
            MySqlCommand sqlCommand = conn.CreateCommand();
            sqlCommand.CommandText = cmdText;
            sqlCommand.Parameters.AddRange(sqlParameters);
            MySqlDataAdapter adapter = new MySqlDataAdapter(sqlCommand);
            adapter.Fill(ds);
            return ds.Tables[0];
        }

        public static int ExecuteNonQuery(string cmdText, params MySqlParameter[] sqlParameters)
        {
            using MySqlConnection conn = new MySqlConnection(_connectionString);
            conn.Open();
            MySqlCommand sqlCommand = conn.CreateCommand();
            sqlCommand.CommandText = cmdText;
            sqlCommand.Parameters.AddRange(sqlParameters);
            return sqlCommand.ExecuteNonQuery();
        }
    }


}
